package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:网页聊天室注册测试
 * Author: WJW
 * Date: 2024-10-24
 * Time: 19:37
 */
public class RegisterPage extends Utils {
    public static String url = "http://127.0.0.1:8080/register.html";

    public RegisterPage() {
        super(url);
    }

    //检查页面是否加载成功
    public void RegisterPageRight(){
        driver.findElement(By.cssSelector("body > div.nav"));
        driver.findElement(By.cssSelector("body > div.login-container > div > h3"));
    }

    public void RegisterSuc() throws InterruptedException {
        //用户名
        driver.findElement(By.cssSelector("#username")).sendKeys("qccen");
        //密码
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        //注册
        driver.findElement(By.cssSelector("#submit")).click();
        Thread.sleep(1000);
        //处理弹窗
        Alert alert = driver.switchTo().alert();
        //点击确认
        alert.accept();
        //校验注册是否成功并返回登录页面
        String title = driver.getTitle();
        assert title.equals("登录页面");
    }

    //注册失败--用户名已存在
    public void RegisterFail01() throws InterruptedException, IOException {
        //用户名
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        //密码
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        //注册
        driver.findElement(By.cssSelector("#submit")).click();
        //对当前异常进行截图
        getScreenShot(getClass().getName());
        Thread.sleep(1000);
        //处理弹窗
        Alert alert = driver.switchTo().alert();
        //点击确认
        alert.accept();
        //刷新页面
        driver.navigate().refresh();
    }

    //注册失败---用户名/密码为空
    public void RegisterFail02() throws InterruptedException, IOException {
        //用户名
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        //密码
        driver.findElement(By.cssSelector("#password"));
        //注册
        driver.findElement(By.cssSelector("#submit")).click();

        Thread.sleep(1000);
        //处理弹窗
        Alert alert = driver.switchTo().alert();
        //点击确认
        alert.accept();
        //对当前异常进行截图
        getScreenShot(getClass().getName());
        //刷新页面
        driver.navigate().refresh();
    }
}
